/**
 *  StaDiUM - State Diagram Unified Modeler
 *  Copyright (C) 2007  Stefan Bucur, Mihai Balan, Claudiu Tanase
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 *  File Information
 *
 *  Last Modified On: $Date$
 *  Last Modified By: $Author$
 */
package ro.pub.cs.stadium.platform.general;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * @author stefan
 *
 */
public abstract class OkCancelDialog implements SelectionListener{
	private final Shell mainWnd;
	private final Shell wndShell;
	
	private final Button okButton;
	private final Button cancelButton;
	
	private final Label buttonSep;
	
	protected abstract void doOkAction();
	protected abstract void doCancelAction();
	
	protected int modalResult = -1;
	
	public OkCancelDialog(Shell mainWnd, boolean haveSeparator) {
		this.mainWnd = mainWnd;
		wndShell = new Shell(mainWnd, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL);
		
		okButton = new Button(wndShell, SWT.PUSH);
		okButton.setText("&OK");
		
		cancelButton = new Button(wndShell, SWT.PUSH);
		cancelButton.setText("&Cancel");
		
		FormLayout layout = new FormLayout();
		layout.marginWidth = layout.marginHeight = 10;
		wndShell.setLayout(layout);
		
		FormData okData = new FormData();
		okData.bottom = new FormAttachment(100);
		okData.width = 100;
		okData.right = new FormAttachment(cancelButton, -10);
		okButton.setLayoutData(okData);
		
		FormData cancelData = new FormData();
		cancelData.right = new FormAttachment(100);
		cancelData.bottom = new FormAttachment(100);
		cancelData.width = 100;
		cancelButton.setLayoutData(cancelData);	
		
		if (haveSeparator) {
			//	Button separator
			buttonSep = new Label(wndShell, SWT.SEPARATOR | SWT.SHADOW_OUT
					| SWT.HORIZONTAL);
			
			FormData sepData = new FormData();
			sepData.left = new FormAttachment(0);
			sepData.right = new FormAttachment(100);
			sepData.bottom = new FormAttachment(okButton, -10);
			sepData.height = 2;
			buttonSep.setLayoutData(sepData);
		} else {
			buttonSep = null;
		}
			
		
		okButton.addSelectionListener(this);
		cancelButton.addSelectionListener(this);
		
		wndShell.setDefaultButton(okButton);
	}
	
	// SelectionListener Interface
	
	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}
	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	public void widgetSelected(SelectionEvent e) {
		if (e.widget == okButton)
			doOkAction();
		
		if (e.widget == cancelButton)
			doCancelAction();
		
	}
	
	// Public methods
	
	public int showModal() {
		modalResult = -1;
		
		wndShell.open();
		
		while (!wndShell.isDisposed ()) {
			if (!wndShell.getDisplay().readAndDispatch ())
				wndShell.getDisplay().sleep ();
		}
		
		return modalResult;
	}
	
	public Shell getMainWnd() {
		return mainWnd;
	}
	
	public Shell getWndShell() {
		return wndShell;
	}
	
	public Button getOkButton() {
		return okButton;
	}
	
	public Button getCancelButton() {
		return cancelButton;
	}
	
	public Label getButtonSep() {
		return buttonSep;
	}
	
	
}
